import 'package:json_annotation/json_annotation.dart';
import 'package:family_app/src/features/gallery/models/photo_model.dart';

part 'album_model.g.dart';

/// 相册模型
@JsonSerializable()
class AlbumModel {
  /// 相册ID
  final String id;

  /// 相册名称
  final String name;

  /// 相册封面图片URL
  final String? coverUrl;

  /// 相册描述
  final String? description;

  /// 相册创建时间
  @JsonKey(name: 'created_at')
  final DateTime createdAt;

  /// 相册更新时间
  @JsonKey(name: 'updated_at')
  final DateTime updatedAt;

  /// 相册中的照片数量
  @JsonKey(name: 'photo_count')
  final int photoCount;

  /// 相册中的照片列表（可选，不一定加载）
  @JsonKey(includeIfNull: false)
  final List<PhotoModel>? photos;

  /// 构造函数
  AlbumModel({
    required this.id,
    required this.name,
    this.coverUrl,
    this.description,
    required this.createdAt,
    required this.updatedAt,
    required this.photoCount,
    this.photos,
  });

  /// 从JSON创建对象
  factory AlbumModel.fromJson(Map<String, dynamic> json) =>
      _$AlbumModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$AlbumModelToJson(this);

  /// 创建新相册
  factory AlbumModel.create({
    required String name,
    String? description,
  }) {
    final now = DateTime.now();
    return AlbumModel(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      name: name,
      description: description,
      createdAt: now,
      updatedAt: now,
      photoCount: 0,
    );
  }

  /// 复制并更新属性
  AlbumModel copyWith({
    String? name,
    String? coverUrl,
    String? description,
    DateTime? updatedAt,
    int? photoCount,
    List<PhotoModel>? photos,
  }) {
    return AlbumModel(
      id: this.id,
      name: name ?? this.name,
      coverUrl: coverUrl ?? this.coverUrl,
      description: description ?? this.description,
      createdAt: this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      photoCount: photoCount ?? this.photoCount,
      photos: photos ?? this.photos,
    );
  }
}
